// pages/hospital/navigator.js
Page({
  data: {
    // 医院列表数据
    hospitals: [
      { 
        id: 1, 
        name: "三甲综合医院", 
        address: "北京市海淀区中关村大街1号", 
        distance: 2.5,
        level: "三级甲等",
        phone: "010-12345678",
        openTime: "08:00-17:30",
        lat: 39.9801,
        lng: 116.3075,
        tags: ["综合医院", "医保定点", "24小时急诊"]
      },
      { 
        id: 2, 
        name: "儿童医院", 
        address: "北京市西城区南礼士路56号", 
        distance: 4.2,
        level: "三级甲等",
        phone: "010-88888888",
        openTime: "08:00-20:00",
        lat: 39.9138,
        lng: 116.3425,
        tags: ["专科医院", "儿科", "医保定点"]
      },
      { 
        id: 3, 
        name: "中医院", 
        address: "北京市东城区东直门内南小街16号", 
        distance: 5.7,
        level: "三级甲等",
        phone: "010-99999999",
        openTime: "08:00-19:00",
        lat: 39.9502,
        lng: 116.4114,
        tags: ["中医", "医保定点", "康复理疗"]
      },
      { 
        id: 4, 
        name: "社区卫生服务中心", 
        address: "北京市海淀区中关村街道社区服务中心", 
        distance: 1.8,
        level: "一级",
        phone: "010-77777777",
        openTime: "08:30-17:00",
        lat: 39.9850,
        lng: 116.3000,
        tags: ["社区医疗", "疫苗接种", "体检"]
      },
      { 
        id: 5, 
        name: "口腔医院", 
        address: "北京市朝阳区建国路88号", 
        distance: 3.6,
        level: "三级甲等",
        phone: "010-66666666",
        openTime: "09:00-18:00",
        lat: 39.9042,
        lng: 116.4134,
        tags: ["专科医院", "口腔科", "正畸"]
      }
    ],
    
    // 地图配置
    mapLat: 39.9801,
    mapLng: 116.3075,
    markers: [],
    scale: 14,
    
    // 筛选条件
    filter: {
      level: '',
      distance: '',
      tags: []
    },
    
    // 加载状态
    isLoading: false,
    hasMore: true,
    page: 1,
    pageSize: 10
  },
  
  onLoad(options) {
    // 初始化地图标记点
    this.initMapMarkers();
    
    // 模拟加载更多数据（实际项目中可以根据需要删除）
    setTimeout(() => {
      this.setData({
        hospitals: [...this.data.hospitals, ...this.data.hospitals]
      });
    }, 1000);
  },
  
  // 初始化地图标记点
  initMapMarkers() {
    const markers = this.data.hospitals.map((hospital, index) => {
      return {
        id: hospital.id,
        latitude: hospital.lat,
        longitude: hospital.lng,
        title: hospital.name,
        iconPath: '/images/hostipal/location_fill.png', // 自定义地图标记图标
        width: 30,
        height: 30,
        callout: {
          content: hospital.name,
          color: '#333',
          fontSize: 14,
          borderRadius: 4,
          bgColor: '#fff',
          padding: 8,
          display: 'ALWAYS'
        }
      };
    });
    
    this.setData({ markers });
  },
  
  // 点击医院项，跳转到医院详情页
  navigateToHospitalDetail(e) {
    const hospitalId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/hospital/detail?id=${hospitalId}`
    });
  },
  
  // 点击导航按钮，调用微信地图导航
  navigateInMap(e) {
    const { lat, lng, name, address } = e.currentTarget.dataset;
    
    wx.openLocation({
      latitude: lat,
      longitude: lng,
      name: name,
      address: address,
      scale: 18
    });
  },
  
  // 切换收藏状态
  toggleFavorite(e) {
    const index = e.currentTarget.dataset.index;
    const hospitals = [...this.data.hospitals];
    hospitals[index].isFavorite = !hospitals[index].isFavorite;
    
    this.setData({ hospitals });
    
    // 提示用户
    wx.showToast({
      title: hospitals[index].isFavorite ? '已收藏' : '已取消收藏',
      icon: 'none'
    });
  },
  
  // 筛选相关方法
  onLevelFilterChange(e) {
    const level = e.detail.value;
    this.setData({
      'filter.level': level
    });
    this.loadHospitals();
  },
  
  onDistanceFilterChange(e) {
    const distance = e.detail.value;
    this.setData({
      'filter.distance': distance
    });
    this.loadHospitals();
  },
  
  onTagFilterChange(e) {
    const { tag } = e.currentTarget.dataset;
    const tags = [...this.data.filter.tags];
    
    if (tags.includes(tag)) {
      const index = tags.indexOf(tag);
      tags.splice(index, 1);
    } else {
      tags.push(tag);
    }
    
    this.setData({
      'filter.tags': tags
    });
    this.loadHospitals();
  },
  
  // 加载医院数据（实际项目中替换为API请求）
  loadHospitals() {
    this.setData({ isLoading: true });
    
    // 模拟API请求
    setTimeout(() => {
      // 这里应根据筛选条件从服务器获取数据
      // 示例中仅使用本地数据
      
      this.setData({
        isLoading: false,
        hasMore: false // 模拟没有更多数据
      });
    }, 800);
  },
  
  // 下拉刷新
  onPullDownRefresh() {
    this.setData({
      page: 1,
      hasMore: true
    });
    
    this.loadHospitals();
    wx.stopPullDownRefresh();
  },
  
  // 上拉加载更多
  onReachBottom() {
    if (this.data.isLoading || !this.data.hasMore) return;
    
    this.setData({
      page: this.data.page + 1,
      isLoading: true
    });
    
    // 模拟加载更多数据
    setTimeout(() => {
      this.setData({
        hospitals: [...this.data.hospitals, ...this.data.hospitals.slice(0, 3)],
        isLoading: false,
        hasMore: false // 模拟没有更多数据
      });
    }, 1000);
  },
  
  // 分享功能
  onShareAppMessage() {
    return {
      title: '附近医院导航',
      path: '/pages/hospital/navigator',
      success: function(res) {
        wx.showToast({
          title: '分享成功',
          icon: 'success'
        });
      }
    };
  }
});